﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufModelViewElementVisibilityFilter.h"
#include "GnufModelViewVisibilityFilterEventHandler.h"
#include "GnufModelViewVisibilityFilterUtils.h"
#include "GnufViewDepthUtils.h"
#include "DebugMessage.h"
#include "ICanvas.h"
#include "IElementBasicInformation.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "IUiViewEventArgs.h"
#include "IUserTransaction.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;

namespace {
    void InitModelViewVisibilityFilters(IUiView* pUiView)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiView, L"pUiView为空", L"paramodel", L"2024-02-20");
        IUiDocument* pUiDoc = pUiView->GetUiDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDoc, L"pUiDoc为空", L"paramodel", L"2024-02-20");
        IDocument* pDoc = pUiDoc->GetDbDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");
        IModelView* pModelView = pUiView->GetModelView();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"pModelView为空", L"paramodel", L"2024-02-20");

        OwnerPtr<IUserTransaction> ut;
        if (!IUserTransaction::IsInUserTransaction(pDoc))
        {
            ut = IUserTransaction::Create(pDoc, GBMP_TR(L"初始化视图显示过滤规则"));
        }
     

        std::vector<std::wstring> ruleNames = GnufModelViewVisibilityFilterUtils::GetModelViewSupportedFilters(pModelView);
        for (const auto& ruleName : ruleNames)
        {
            if (!pModelView->GetElementVisibilityFilter(ruleName))
            {
                OwnerPtr<IElementVisibilityFilter> modelViewVisibilityFilter = GnufModelViewVisibilityFilterUtils::CreateModelViewFilter(ruleName);
                DBG_WARN_AND_CONTINUE_UNLESS(modelViewVisibilityFilter, L"modelViewVisibilityFilter为空", L"paramodel", L"2024-02-20");
                pModelView->AddElementVisibilityFilter(TransferOwnership(modelViewVisibilityFilter));
            }
        }

        // 找出显示过滤规则和该视图一致的其他视图的当前规则；如果没有找到，则使用该视图的默认规则
        std::wstring currentRuleName;
        std::vector<IModelView*> modelViews = IModelView::GetAllModelViews(pDoc);
        FOR_EACH(pOtherModelView, modelViews)
        {
            if (!pOtherModelView)
                continue;

            if (pOtherModelView->GetElementId() == pModelView->GetElementId())
                continue;

            /*if (GnufModelViewVisibilityFilterUtils::CheckIfModelViewShareSameFilter(pOtherModelView->GetUserDataId(), pModelView->GetUserDataId()))
            {
                std::wstring otherModelViewCurrentRuleName = GnufModelViewVisibilityFilterUtils::GetModelViewCurrentActivedFilter(pOtherModelView);
                if (!otherModelViewCurrentRuleName.empty())
                {
                    currentRuleName = otherModelViewCurrentRuleName;
                }
                break;
            }*/
        }

        if (currentRuleName.empty())
        {
            currentRuleName = GnufModelViewVisibilityFilterUtils::GetModelViewDefaultFilter(pModelView);
            DBG_WARN_AND_RETURN_VOID_UNLESS(!currentRuleName.empty(), L"currentRuleInternalName为空", L"paramodel", L"2024-02-20");
        }

        bool bOk = GnufModelViewVisibilityFilterUtils::ActiveModelViewFilter(pModelView, currentRuleName, true);
        DBG_WARN_AND_RETURN_VOID_UNLESS(bOk, L"ActiveModelViewFilter失败", L"paramodel", L"2024-02-20");
        if (ut != nullptr)
        {
            ut->Commit();
        }

        ICanvas* canvas = pUiView->GetCanvas();
        DBG_WARN_AND_RETURN_VOID_UNLESS(canvas, L"canvas为空", L"paramodel", L"2024-02-20");
        canvas->RefreshRenderArea(true);

    }
}

void GnufModelViewVisibilityFilterEventHandler::On(IUiViewEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空", L"paramodel", L"2024-02-20");
    if (pArgs->GetType() == UiViewEventType::UiViewCreated)
    {
        IUiView* pUiView = pArgs->GetUiView();
        if (pUiView)
        {
            InitModelViewVisibilityFilters(pUiView);
        }
    }
}
